<?php

namespace App\Services\Transfer;

use App\Models\Video;
use Illuminate\Support\Facades\DB;
use Darabonba\OpenApi\OpenApiClient;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;

use Darabonba\OpenApi\Models\Config;
use Darabonba\OpenApi\Models\Params;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\OpenApiRequest;
use App\Jobs\VideoTranslateJob;

class TransferVideo
{
    protected Video $video;

    public function __construct(Video $video)
    {
        $this->video = $video;
    }


    public static function createClient(){
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html。
        $config = new Config([
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
            "accessKeyId" => env("ALIYUN_TINGWU_ACCESS_KEY_ID"),
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            "accessKeySecret" => env("ALIYUN_TINGWU_ACCESS_KEY_SECRET")
        ]);
        // Endpoint 请参考 https://api.aliyun.com/product/tingwu
        $config->endpoint = "tingwu.cn-beijing.aliyuncs.com";
        return new OpenApiClient($config);
    }

    /**
     * API 相关
     * @return Params OpenApi.Params
     */
    public static function createApiInfo(){
        $params = new Params([
            // 接口名称
            "action" => "CreateTask",
            // 接口版本
            "version" => "2023-09-30",
            // 接口协议
            "protocol" => "HTTPS",
            // 接口 HTTP 方法
            "method" => "PUT",
            "authType" => "AK",
            "style" => "ROA",
            // 接口 PATH
            "pathname" => "/openapi/tingwu/v2/tasks",
            // 接口请求体内容格式
            "reqBodyType" => "json",
            // 接口响应体内容格式
            "bodyType" => "json"
        ]);
        return $params;
    }

    /**
     * @param string[] $args
     * @return void
     */
    public function main(){
        $client = self::createClient();
        $params = self::createApiInfo();

       
       if(!strstr($this->video->video_url,"http")){

          $video = "https://littleshop.oss-cn-beijing.aliyuncs.com".$this->video->video_url;
       }else{

          $video = $this->video->video_url;
       }
        // query params
        $queries = [];
        $queries["type"] = "offline";
        // body params
        $body = [
            "Input" => [
                "FileUrl" => $video,
                "SourceLanguage" => "cn"
            ],
            "Parameters" => [
                "Translation" => [
                    "TargetLanguages" => [
                        "cn"
                    ],
                    "OutputLevel" => 1
                ],
                "TranslationEnabled" => true,
                "MeetingAssistanceEnabled" => false,
                "SummarizationEnabled" => true,
                "Summarization" => [
                    "Types" => [
                        "Paragraph"
                    ]
                ]
            ],
            "AppKey" => "FPzj7g7IeJj7YKn6"
        ];
        // runtime options
        $runtime = new RuntimeOptions([]);
        $request = new OpenApiRequest([
            "query" => OpenApiUtilClient::query($queries),
            "body" => $body
        ]);
        // 复制代码运行请自行打印 API 的返回值
        
        // 返回值实际为 Map 类型，可从 Map 中获得三类数据：响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
        $res = $client->callApi($params, $request, $runtime);

        
        if($res['body']['Message'] == 'success'){

            $this->video->update(['task_id'=>$res['body']['Data']['TaskId']]);

            dispatch(new VideoTranslateJob($this->video))->onQueue('video');

        }
    }

    public function transfer()
    {

         //dispatch(new VideoTranslateJob($this->video))->onQueue('video');die;
        $videos = $this->main();
    }

   
}
